package com.apes.fa.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.stock.model.Depot;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @description: 资产借还单
 * @author: PengZiFeng
 * @create: 2021-09-10 16:21
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaAssetBorrowReturnSingle.CONST_TABLE_NAME)
public class FaAssetBorrowReturnSingle extends Model {

    public final static  String CONST_TABLE_NAME = "fa_asset_borrow_return_single";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产借还单", prefix = "ZJ", padding = 5)
    @Comment("资产借还单号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "lend_dept_id", referencedColumnName = "id")
    @Comment("借出部门")
    private Dept lendDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "borrow_dept_id", referencedColumnName = "id")
    @Comment("借用部门")
    private Dept borrowDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "lend_depot_id", referencedColumnName = "id")
    @Comment("借出仓库")
    private Depot lendDepot;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "borrow_depot_id", referencedColumnName = "id")
    @Comment("借入仓库")
    private Depot borrowDepot;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fa_card_id", referencedColumnName = "id")
    @Comment("资产卡片")
    private FaCard faCard;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "old_principal_id", referencedColumnName = "id")
    @Comment("原负责人")
    private PartyRole oldPrincipal;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "old_custodian_id", referencedColumnName = "id")
    @Comment("原保管人")
    private PartyRole oldCustodian;

    @Column(name = "old_status")
    @Comment("原资产状态")
    private String oldStatus;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "principal_id", referencedColumnName = "id")
    @Comment("负责人")
    private PartyRole principal;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "custodian_id", referencedColumnName = "id")
    @Comment("保管人")
    private PartyRole custodian;

    @Column(name = "lend_note")
    @Comment("借出备注")
    private String lendNote;

    @Column(name = "return_note")
    @Comment("归还备注")
    private String returnNote;

    @Column(name = "business_state")
    @Comment("单据状态")
    private String businessState;

    @Transient
    private String faCategoryId;

    public String getFaCategoryId() {
        if (faCard!=null) return faCard.getFaCategory().getId();
        return null;
    }

    @Transient
    private String productId;

    public String getProductId() {
        if (faCard!=null) return faCard.getProduct().getId();
        return null;
    }

    @Transient
    private String productName;

    public String getProductName() {
        if (faCard!=null) return faCard.getProduct().getName();
        return null;
    }


    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
